package com.hhp.cloud.shop.admin.dao;

import com.alibaba.fastjson.JSONObject;
import com.hhp.cloud.shop.admin.pojo.dto.AuthUserDTO;
import com.hhp.cloud.shop.admin.pojo.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户信息表
 * 
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-23 10:25:32
 */
@Mapper
public interface UserDao extends BaseMapper<UserEntity> {

    AuthUserDTO getByUsername(String username);

    @Update({
            "<script> \n" ,
                    "UPDATE sys_user \n" ,
                    "<trim prefix='set' suffixOverrides=','> \n" ,
                    " <trim prefix='username = case' suffix='end,'> \n" ,
                    "  <foreach collection='list' item='item' index='index'> \n" ,
                    "   <if test='item.username != null'> \n" ,
                    "    when id=#{item.id} then #{item.username} \n" ,
                    "   </if> \n" ,
                    "  </foreach> \n" ,
                    " </trim> \n" ,

                    " <trim prefix='gmt_modified = case' suffix='end,'> \n" ,
                    "  <foreach collection='list' item='item' index='index'> \n" ,
                    "   <if test='item.gmtModified != null'> \n" ,
                    "    when id=#{item.id} then #{item.gmtModified} \n" ,
                    "   </if> \n" ,
                    "  </foreach> \n" ,
                    " </trim> \n" ,

                    /*" <trim prefix='avatar = case' suffix='end,'> \n" ,
                    "  <foreach collection='list' item='item' index='index'> \n" ,
                    "   <if test='item.avatar != null'> \n" ,
                    "    when id=#{item.id} then #{item.avatar} \n" ,
                    "   </if> \n" ,
                    "  </foreach> \n" ,
                    " </trim> \n" ,*/

                    "</trim> " ,
                    "where ID in" ,
                    " <foreach collection='list' separator=',' item='item' index='index' open='(' close=')'> \n" ,
                    "  #{item.id} \n" ,
                    " </foreach> \n" ,
                    "</script> "
    })
    int updateBatch(@Param("list") List<UserEntity> userEntities);

    @Insert({"<script>" +
            "INSERT INTO sys_user (id,username,avatar,gmt_create) VALUES " ,
            "<foreach collection='list' item='item' separator=','>" ,
            "(#{item.id},#{item.username},#{item.avatar},DATE_FORMAT(NOW(),'%Y%m%d%H%i%s'))" ,
            "</foreach>" ,
            "</script>"})
    void insertBatchUsers(@Param("list") List<UserEntity> userEntities);
}
